const Toast = require('../../dist/toast/toast');
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    send: false,
    send1: true,
    qu: true,
    qu1: false,
    clickTimes:'true',
    duration: 3000,
    current: 3, //0送餐 9店内就餐
    id: '',
    $zanui: {
      toptips: {
        show: false
      }
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var id = options.id
    this.setData({
      id: id
    })
  },
  haha(){
    wx.getStorageInfo({
      success: function (res) {
        console.log(res.key)
      }
    })
  },
  onShow(){
    this.pageLoading = !1;
  },
  tap(e) {
    let that = this
    let index = e.currentTarget.dataset.id
    console.log(index)
    if (index == 1) {
      return false;
    } else {
      that.setData({
        qu: false,
        qu1: true,
        send: true,
        send1: false,
        current: index
      })
    }
  },
  tap1(e) {
    let that = this
    let index = e.currentTarget.dataset.id
    if (index == 1) {
      return false;
    } else {
      console.log(index)
      that.setData({
        qu: true,
        qu1: false,
        send: false,
        send1: true,
        current: index
      })
    }
  },
  //点击倒计时
  clickTime(){
    var that=this
    setTimeout(() => {
      this.setData({
       clickTimes:'true'
      });
    }, 2000);

  },
  //判断登录状态
  isLogin(){
     var that=this
    let userid = app.globalDataO.openid.objectSid
     wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo'] && userid != "" || userid != null) {
          that.goDing()
        } else {
    
          console.log('没授权')
         that.getuserInfo1()
         
        }
      },
    })
  },
//进入餐厅、
goDing(){
  var that=this
  console.log('没进来',that.data.clickTimes)
  if (that.data.clickTimes == 'true') {
    console.log('进来后',that.data.clickTimes)
    let restaurantId = that.data.id
    let userid = app.globalDataO.openid.objectSid
    let deskId = 0
    that.data.clickTimes = 'false'
    that.clickTime()
    console.log(userid)
    if (!that.data.current) {
      wx.showToast({
        icon: 'fail',
        title: '请选择就餐方式',
      })
    }
    if (that.data.current == 3) {
      app.post(app.url.DCopenTable, {
        restaurantId: restaurantId,
        memberId: userid,
        deskId: deskId,
        type: 4,
        seats: 0
      }).then(res => {
        console.log(res)
        let bill_id = res.data.bill_id
        if (bill_id == -1) {
          wx.showToast({
            title: '暂无法点餐',
            image: "../../images/gantan.png"
          })
          return false;
        }

          wx.navigateTo({
            url: '../../pages/food/food?restaurantId=' + restaurantId + '&types=4' + '&bill_id=' + bill_id + '&deskId=' + deskId,
          })
        
      })
    }
    if (that.data.current == 2) {
      wx.navigateTo({
        url: '../../pages/select_table/select_table?restaurantId=' + restaurantId + '&types=' + that.data.current
      })
    }
  }else{
    console.log('请不要多次点击')
  }

},
  getUserid() {
    let that = this;
    wx.login({
      success: res => {
        // console.log(res)
        if (res.code) {
          wx.request({
            url: app.url.logins,
            data: {
              code: res.code
            },
            header: {
              "Content-Type": "application/x-www-form-urlencoded"
            },
            method: 'POST',
            success: res => {
              var openid = res.data
              var sessionKey = res.data.sessionKey
              that.getUser(openid, sessionKey)
              //储存返回的openid
            }
          })
        } else {
          // console.log('获取用户登录态失败！' + res.errMsg);
          reject('error');
        }
      }
    })
  },
  // 获取用户信息
  getUser: function (openid, sessionKey) {
    var that = this
    return new Promise(function (resolve, reject) {
      wx.getUserInfo({
        success: res => {
          resolve(res)
          console.log(res)
          if (res.userInfo) {
            var encryptedData = res.encryptedData
            var iv = res.iv
            return new Promise(function (resolve, reject) {
              wx.request({
                url: app.url.getuserId,
                method: 'GET',
                header: {
                  'content-type': 'application/json',
                },
                data: {
                  encryptedData: encryptedData,
                  sessionKey: sessionKey,
                  iv: iv,
                  userId: ''
                },
                success(res) {
                  app.globalDataO.openid['objectSid'] = res.data.objectSid
                  that.goDing()
                }
              })
            })
          } else {
            wx.showToast({
              title: '网络出错',
            })
          }
          // console.log(that.globalData.userInfo)
          if (that.userInfoReadyCallback) {
            that.userInfoReadyCallback(res)
          }
        },
      })



    })
  },
//判断授权
  getuserInfo1: function (e) {
    var that=this
    wx.getUserInfo({
        success: res => {
          that.getUserid()
        },
    })
    

  
  },
  btn_e() {
    var that = this
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          that.goDing()
        } else {
          that.getuserInfo1()
        }
      },
    })
  },
  _topCue: function (text) {
    this.setData({
      content: text,
      $zanui: {
        toptips: {
          show: true
        }
      }
    });
    setTimeout(() => {
      this.setData({
        $zanui: {
          toptips: {
            show: false
          }
        }
      });
    }, this.data.duration);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})